using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Terra.Core.Infrastructure.SMS.Interfaces;
using Terra.Core.Infrastructure.SMS.Options;

namespace Terra.Core.Infrastructure.SMS.Services;

/// <summary>
/// 默认短信发送服务实现
/// 这是一个简单的实现，实际项目中应该替换为具体的短信服务商SDK
/// </summary>
public class DefaultSmsSender : ISmsSender
{
    private readonly SmsOptions _options;
    private readonly ILogger<DefaultSmsSender> _logger;
    private readonly HttpClient _httpClient;

    public DefaultSmsSender(
        IOptions<SmsOptions> options,
        ILogger<DefaultSmsSender> logger,
        HttpClient httpClient)
    {
        _options = options.Value;
        _logger = logger;
        _httpClient = httpClient;
    }

    /// <summary>
    /// 发送短信
    /// </summary>
    public async Task<bool> SendAsync(string phoneNumber, string content, string templateCode = null)
    {
        try
        {
            // 这里应该实现具体短信服务商的API调用
            // 这只是一个示例实现
            _logger.LogInformation(
                "Sending SMS to {PhoneNumber} with content: {Content}",
                phoneNumber,
                content
            );

            // 模拟发送过程
            await Task.Delay(100);

            _logger.LogInformation("SMS sent successfully to {PhoneNumber}", phoneNumber);
            return true;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to send SMS to {PhoneNumber}", phoneNumber);
            return false;
        }
    }
}